import { getClusterList, deleteCluster } from "@/api/vap/cluster";
import { ref, computed, onMounted } from "vue";
import { useRouter } from "vue-router";

export function useCluster() {
  const dialogFormAddRef = ref();
  const router = useRouter();
  const dataList = ref([]);
  const loading = ref(true);
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      minWidth: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "集群名称",
      prop: "name",
      align: "center"
    },
    {
      label: "集群类型",
      prop: "clusterType",
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={
            row.clusterType === 1
              ? "success"
              : row.clusterType === 2
              ? "info"
              : "danger"
          }
          effect="plain"
        >
          {row.clusterType === 1
            ? "私有"
            : row.clusterType === 2
            ? "抢占"
            : "共享"}
        </el-tag>
      )
    },
    {
      label: "CPU阈值",
      prop: "cpuThreshold",
      align: "center"
    },
    {
      label: "内存阈值",
      prop: "memoryThreshold",
      align: "center"
    },
    {
      label: "最大会话时长",
      prop: "maxSessionTime",
      align: "center"
    },
    {
      label: "会话超时时间",
      prop: "maxTimeOutTime",
      align: "center"
    },
    {
      label: "超时策略",
      prop: "timeOutAction",
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.timeOutAction === 1 ? "success" : "info"}
          effect="plain"
        >
          {row.timeOutAction === 1 ? "注销会话" : "断开会话"}
        </el-tag>
      )
    },
    {
      label: "操作",
      fixed: "right",
      width: 160,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });
  function handleDelete(row: any) {
    console.log(row);
    deleteCluster(row.id).then(() => {
      onSearch();
    });
  }
  function handleAppIssue(row: any) {
    router.push("/veapp/appIssue/index?id=" + row.id);
  }
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  async function onSearch() {
    loading.value = true;
    getClusterList().then(res => {
      dataList.value = res;
      setTimeout(() => {
        loading.value = false;
      }, 500);
    });
  }

  onMounted(() => {
    onSearch();
  });
  return {
    dialogFormAddRef,
    loading,
    columns,
    dataList,
    buttonClass,
    onSearch,
    resetForm,
    handleDelete,
    handleAppIssue
  };
}
